#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>

using namespace std;

class Card
{
public:
	int familly;
	int number;
	int place;
	int show;
};

void init();
int card_rand();
void print_card();
void card_sort();

const int num_fam=4;
const int num_num=13;
const int num_cards=num_fam*num_num;
const int cpp=5;
const int num_p=4;
int player;
Card **cards;
typedef Card* pCard;
int arr[num_cards]={0,};
char input;

int main(int argc, char** argv)
{
	init();
	printf("%d, %d, %d, %d\n", num_fam, num_num, cpp, num_p);
	player=0;
	 // program main loop
    bool done = false;
    while (!done)
    {
        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = true;
                    break;
                }
            } // end switch
        } // end of message processing

        // DRAWING STARTS HERE

		printf("바꾸고 싶은 카드 번호를 입력하세요");
		print_card();
        // clear screen
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

        // draw bitmap
        SDL_BlitSurface(bmp, 0, screen, &dstrect);

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(screen);
    } // end main loop

    // free loaded bitmap
    SDL_FreeSurface(bmp);

    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}


int card_rand()
{
	int rana;

	for(int i=0;i<num_cards; i++)
	{
		rana = i+(rand()%(num_cards-i));
		int prev;

		prev = arr[i];
		arr[i] = arr[rana];
		arr[rana] = prev;
	}
	//if(!flag)
	return 0;
}
void init()
{
	cards=new pCard[52];
	player=0;
	for(int i=0; i< num_cards; i++)
	{
		cards[i]=new Card;
		arr[i]=i;
	}
	card_rand();
	for(int i=0; i<num_fam; i++)
	{
		for(int j=0; j<num_num; j++)
		{
			cards[i*num_num+j]->familly=i;
			cards[i*num_num+j]->number=j;
			cards[i*num_num+j]->place=arr[i*num_num+j];
			cards[i*num_num+j]->familly=0;
		}
	}
	card_sort();
}

void print_card()
{
	/*
	for(int i=0; i<num_fam; i++)
	{
		for(int j=0; j<num_num; j++)
		{
			printf("%d, %d\n",i*num_num+j,cards[i*num_num+j]->place);
		}
	}
*/
	for(int i=0; i<cpp; i++)
	{
			printf("player %d\n", player);
			printf("%d, %d\n",player*cpp+i,cards[player*cpp+i]->place);
	}
}

void card_sort()
{
	Card *t;
	for(int i=0; i<num_cards; i++)
	{
		for(int j=0; j<num_cards-1; j++)
		{
			if(cards[j]->place > cards[j+1]->place)
			{
				t=cards[j];
				cards[j]=cards[j+1];
				cards[j+1]=t;
			}
		}
	}
}
